package com.yang.news.job.jobhandler;


import com.google.common.collect.Lists;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yang.convert.HotSearchConvert;
import com.yang.news.model.dto.HotSearchDetailDTO;
import com.yang.news.dao.entity.HotSearchRecord;
import com.yang.news.service.HotSearchRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

import static com.yang.news.model.cache.HotSearchCache.CACHE_MAP;
import static com.yang.news.enums.HotSearchEnum.BAIDU;


/**
 * 百度热搜Java爬虫代码
 */
@Component
@Slf4j
public class BaiduHotSearchJob {

    @Autowired
    private HotSearchRecordService hotSearchRecordService;

    @PostConstruct
    public void init(){
        try{
            hotSearch(null);
            log.info("初始化百度数据成功");
        }catch (Exception e){
            log.error("初始化百度数据异常",e);
        }
    }

    @XxlJob("baiduHotSearchJob")
    public ReturnT<String> hotSearch(String param){
         log.info("百度热搜爬虫任务开始");
        try {
            //获取百度热搜
            String url = "https://top.baidu.com/board?tab=realtime&sa=fyb_realtime_31065";
            List<HotSearchRecord> hotSearchList = Lists.newArrayList();
            Document doc = Jsoup.connect(url).get();
            //标题
            Elements titles = doc.select(".c-single-text-ellipsis");
            //图片
            Elements imgs = doc.select(".category-wrap_iQLoo .index_1Ew5p").next("img");
            //内容
            Elements contents = doc.select(".hot-desc_1m_jR.large_nSuFU");
            //推荐图
            Elements urls = doc.select(".category-wrap_iQLoo a.img-wrapper_29V76");
            //热搜指数
            Elements levels = doc.select(".hot-index_1Bl1a");
            for (int i = 0; i < levels.size(); i++) {
                HotSearchRecord hotSearchRecord = HotSearchRecord.builder().hotSearchResource(BAIDU.getCode()).build();
                //设置文章标题
                hotSearchRecord.setHotSearchTitle(titles.get(i).text().trim());
                //设置百度三方ID
                hotSearchRecord.setHotSearchId(getHashId(BAIDU.getDesc() + hotSearchRecord.getHotSearchTitle()));
                //设置文章封面
                hotSearchRecord.setHotSearchCover(imgs.get(i).attr("src"));
                //设置文章摘要
                hotSearchRecord.setHotSearchExcerpt(contents.get(i).text().replaceAll("查看更多>", ""));
                //设置文章连接
                hotSearchRecord.setHotSearchUrl(urls.get(i).attr("href"));
                //设置热搜热度
                hotSearchRecord.setHotSearchHeat(levels.get(i).text().trim());
                //按顺序排名
                hotSearchRecord.setHotSearchOrder(i + 1);
                hotSearchList.add(hotSearchRecord);
            }
            if (CollectionUtils.isEmpty(hotSearchList)) {
                return ReturnT.SUCCESS;
            }
            //数据加到缓存中
            CACHE_MAP.put(BAIDU.getCode(), HotSearchDetailDTO.builder()
                //热搜数据
                .hotSearchDTOList(
                    hotSearchList.stream().map(HotSearchConvert::toDTOWhenQuery).collect(Collectors.toList()))
                //更新时间
                .updateTime(Calendar.getInstance().getTime()).build());
            //数据持久化
            hotSearchRecordService.batchSave(hotSearchList);
            log.info("百度热搜爬虫任务结束");
            return ReturnT.SUCCESS;
        } catch (IOException e) {
            log.error("获取百度数据异常", e);
        }
        return ReturnT.FAIL;
    }

    /**
     * 根据文章标题获取一个唯一ID
     *
     * @param title 文章标题
     * @return 唯一ID
     */
    public static String getHashId(String title) {
        long seed = title.hashCode();
        Random rnd = new Random(seed);
        return new UUID(rnd.nextLong(), rnd.nextLong()).toString();
    }

}

